<?php

namespace App\Http\Controllers;

use DB;

use App\Zixun;
use App\ZxLists;
use App\Zxcomment;
use App\Zacomrep;
use App\Zxbanner;
use App\Ztbanner;
use App\Tool;
use App\Ztfenlei;
use App\Ztlist;
use App\Ztarticle;
use App\Ztcirarticle;

use App\Datatables;
use Illuminate\Http\Request;
use Storage;

class ZixunController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 资讯列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function fenleiList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::FenleiList());
        }
        return view('zixun.fenlei');
    }

    /**
     * 添加分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function fenleiPublish(Request $request)
    {
        if ($request->isMethod('post')) {

            $return = Zixun::FenleiPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/fenlei')->with('status', $return['status'])->with('message', $return['message']);
            } else {
                return back()->with('status', $return['status'])->with('message', $return['message']);
            }
        }

        return view('zixun.fenleiPublish');
    }


    /**
     * 修改分类
     * @param Request $request
     * @param fenlei $fenlei
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function fenleiModify(Request $request, Zixun $zixun)
    {
        if ($request->isMethod('post')) {
            $return = Zixun::FenleiModify($request, $zixun);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.fenleiModify')->with(compact('zixun'));
    }

    /**
     * 删除分类
     * @param fenlei $fenlei
     * @return array
     * @throws \Exception 
     */
    public function fenleiDels($id, Request $request)
    {
		$fenlei = Zixun::where('id', $id)->exists();
        if (!$fenlei) return ['status' => 2, 'message' => '无此分类'];
        return Zixun::Dels($id, $request);
    }
	//刷新分类
	public function fenleiShuaxin($id, Request $request){
		$fenlei = Zixun::where('id', $id)->exists();
        if (!$fenlei) return ['status' => 2, 'message' => '无此分类'];
        return Zixun::Shuaxin($id, $request);		
	}
	
	//banner 列表
    public function bannerList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::BannerzxList());
        }
        return view('zixun.banner');
    }
	//banner 添加
    public function bannerPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = Zxbanner::BannerPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/banner')->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.bannerPublish');
    }
	//banner 修改
    public function bannerModify(Request $request, Zxbanner $Zxbanner)
    {
        if ($request->isMethod('post')) {
            $return = Zxbanner::BannerModify($request, $Zxbanner);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.bannerModify')->with(compact('Zxbanner'));
    }
    /**
     * banner删除
     */
    public function bannerDel(Zxbanner $Zxbanner)
    {
        if (!$Zxbanner->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }
        return ['status' => 1, 'message' => '删除成功'];
    }
	
	//工具 列表
    public function toolList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::toolzxList());
        }
        return view('zixun.tool');
    }
	// 添加工具
	public function toolPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = Tool::ToolPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/tool')->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.toolPublish');
    }
	//修改工具
	public function toolModify(Request $request, Tool $Tool)
	{
        if ($request->isMethod('post')) {
            $return = Tool::ToolModify($request, $Tool);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.toolModify')->with(compact('Tool'));		
	}
	//删除工具
	public function toolDel($id, Request $request)
	{
		$gongju = Tool::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此工具'];
        return Tool::Del($id, $request);
	}
	
	//刷新工具
	public function toolshuaxin($id, Request $request){
		$tool = Tool::where('id', $id)->exists();
        if (!$tool) return ['status' => 2, 'message' => '无此工具'];
        return Tool::Shuaxin($id, $request);		
	}	
	
	//专题分类列表
	public function ztfenleiList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::ZtfenleiList());
        }
        return view('zixun.ztfenlei');
    }
	
	// 添加专题分类
	public function ztfenleiPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = Ztfenlei::ZtfenleiPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/ztfenlei')->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztfenleiPublish');
    }
	//修改专题分类
	public function ztfenleiModify(Request $request, Ztfenlei $Ztfenlei)
	{
        if ($request->isMethod('post')) {
            $return = Ztfenlei::ZtfenleiModify($request, $Ztfenlei);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztfenleiModify')->with(compact('Ztfenlei'));		
	}
	//删除专题分类
	public function ztfenleiDel($id, Request $request)
	{
		$gongju = Ztfenlei::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此分类'];
        return Ztfenlei::Del($id, $request);
	}
	//刷新专题分类
	public function ztflshuaxin($id, Request $request){
		$tool = Ztfenlei::where('id', $id)->exists();
        if (!$tool) return ['status' => 2, 'message' => '无此分类'];
        return Ztfenlei::Shuaxin($id, $request);		
	}
	//专题列表
	public function ztList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::ZtList());
        }
        return view('zixun.ztlist');
    }
	//添加专题
	public function ztlistPublish(Request $request)
    {
        if ($request->isMethod('post')) {
            $return = ZtList::ZtPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/ztlist')->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztPublish');
    }
	
	//修改专题
	public function ztlistModify(Request $request, Ztlist $Ztlist)
	{
        if ($request->isMethod('post')) {
            $return = Ztlist::ZtfenlieibnModify($request, $Ztlist);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztModify')->with(compact('Ztlist'));		
	}
	
	//删除专题
	public function ztlistDel($id, Request $request)
	{
		$gongju = Ztlist::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此内容'];
        return Ztlist::Del($id, $request);
	}	
	
	//刷新专题
	public function ztlistshuaxin($id, Request $request){
		$tool = Ztlist::where('id', $id)->exists();
        if (!$tool) return ['status' => 2, 'message' => '无此专题'];
        return Ztlist::Shuaxin($id, $request);		
	}
	
	
	//专题banner
	public function ztlistBanner(Request $request, Ztlist $Ztlist){
		if($request->isMethod('post')) {
            return response()->json(Datatables::ZtfenleiBanner($Ztlist->id));
        }
        return view('zixun.ztfenleibanner')->with(compact('Ztlist'));
	}
	// 添加专题banner
	public function listbannerPublish(Request $request, Ztlist $Ztlist)
    {

        if ($request->isMethod('post')) {
            $return = Ztbanner::ZtfenleibnPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/listBanner/'.$Ztlist->id)->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztfenleibnPublish')->with(compact('Ztlist'));
    }
	//修改专题banner
	public function listbannerModify(Request $request, Ztbanner $Ztbanner)
	{
        if ($request->isMethod('post')) {
            $return = Ztbanner::ZtfenlieibnModify($request, $Ztbanner);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztfenleibnModify')->with(compact('Ztbanner'));		
	}
	//删除专题banner
	public function listbannerDel($id, Request $request)
	{
		$gongju = Ztbanner::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此分类'];
        return Ztbanner::Del($id, $request);
	}
	
	//添加专题底部文章
	public function artlist(Request $request, Ztlist $Ztlist){
		if($request->isMethod('post')) {
            return response()->json(Datatables::ZtartcleList($Ztlist->id));
        }
        return view('zixun.ztArtlist')->with(compact('Ztlist'));
	}
	// 添加专题底部文章
	public function artPublish(Request $request, Ztlist $Ztlist)
    {
        if ($request->isMethod('post')) {
            $return = Ztarticle::artPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/artlist/'.$Ztlist->id)->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztartPublish')->with(compact('Ztlist'));
    }	
	//修改专题底部文章
	public function artModify(Request $request, Ztarticle $Ztarticle)
	{
        if ($request->isMethod('post')) {
            $return = Ztarticle::artModify($request, $Ztarticle);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztartModify')->with(compact('Ztarticle'));		
	}
	
	//删除专题底部文章
	public function artdel($id, Request $request)
	{
        try {
            if (!DB::table('kl_kind_article')->where('id', $id)->delete()) {
                return ['message' => '删除失败', 'status' => 2];
            }
            return ['message' => '删除成功', 'status' => 1];
        } catch (Exception $exception) {
            return ['message' => '删除失败', 'status' => 2];
        }
	}
	
	//添加专题底部文章
	public function arcirtlist(Request $request, Ztlist $Ztlist){
		if($request->isMethod('post')) {
            return response()->json(Datatables::ZtcarticleList($Ztlist->id));
        }
        return view('zixun.ztcirArtlist')->with(compact('Ztlist'));
	}
	// 添加专题底部文章
	public function artcirPublish(Request $request, Ztlist $Ztlist)
    {
        if ($request->isMethod('post')) {
            $return = Ztcirarticle::artcirPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/arcirtlist/'.$Ztlist->id)->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztcirartPublish')->with(compact('Ztlist'));
    }	
	//修改专题底部文章
	public function artcirtModify(Request $request, Ztcirarticle $Ztcirarticle)
	{
        if ($request->isMethod('post')) {
            $return = Ztcirarticle::artcirModify($request, $Ztcirarticle);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.ztcirartModify')->with(compact('Ztcirarticle'));		
	}
	
	//删除专题底部文章
	public function artcirdel($id, Request $request)
	{
        try {
            if (!DB::table('kl_kind_circle_article')->where('id', $id)->delete()) {
                return ['message' => '删除失败', 'status' => 2];
            }
            return ['message' => '删除成功', 'status' => 1];
        } catch (Exception $exception) {
            return ['message' => '删除失败', 'status' => 2];
        }
	}
	
	//资讯列表
    public function zxList(Request $request)
    {
		if($request->isMethod('post')) {
            return response()->json(Datatables::ZxsList());
        }
        return view('zixun.zxlist');
    }
	
	// 添加资讯文章
	public function zxlistPublish(Request $request, ZxLists $ZxLists)
    {
        if ($request->isMethod('post')) {
            $return = ZxLists::zxartPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/zxlist/'.$ZxLists->id)->with('status', $return['status'])->with('message', $return['message']);
            }
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
        return view('zixun.zxPublish')->with(compact('ZxLists'));
    }	
	
	//资讯定时发送添加文章
	public function zxtimePublish(Request $request){

		//如果是post方法为发帖
        if ($request->isMethod('post')) {
           $return = ZxLists::zxartPublish($request);
		   if ($return['status'] === 1) {
                return redirect('/zixun/timeposing')->with('status', $return['status'])->with('message', $return['message']);
            }
        }

        return view('zixun.zxtposingPublish');		
	}

	//修改资讯文章
	public function zxlistModify($article_id, Request $request)
	{ 
		$advice = ZxLists::find($article_id);

        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        if ($request->isMethod('post')) {

            $return = ZxLists::zxartcirModify($advice,$request);
            return back()->with('status', $return['status'])->with('message', $return['message']);
        }
		//封面图
        $cover = DB::table('kl_article_pic')->select('id', 'pic_path')->where('article_id', $advice->id)->get()->toArray();

        return view('zixun.zxModify')->with(compact('advice','cover'));		
	}
	
    public function picDel($p_id) 
    {
        if (DB::table('kl_article_pic')->where('article_id', DB::table('kl_article_pic')->where('id', $p_id)->value('article_id'))->count() <= 1) {
            return response()->json(['message' => '至少保留一张', 'status' => 2]);
        }
        return DB::table('kl_article_pic')->where('id', $p_id)->delete() ? response()->json(['message' => '删除成功', 'status' => 1]) : response()->json(['message' => '删除失败', 'status' => 2]);
    }
	
	//删除资讯文章
	public function zxlistDel($id, Request $request)
	{
		$gongju = ZxLists::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此数据'];
        return ZxLists::Del($id, $request);
	}
	
	public function chexiaodel($id){
        //是否存在该帖子
        if (!DB::table('kl_article')->where('id', $id)->exists()) {
            return ['status' => 2, 'message' => '资讯不存在'];
        }
        //删除帖子
        if (!DB::table('kl_article')->where('id', $id)->delete()) {
            return ['status' => 2, 'message' => '撤销失败'];
        }

        return ['status' => 1, 'message' => '撤销成功'];		
	}
	
    /**
     * 设置热门
     * @param $advice_id
     * @return array
     */
    public function zxHot($advice_id)
    {
        $advice = ZxLists::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->hot = ZxLists::HOT_YES;
		$advice->excellent_time = date("Y-m-d H:i:s");
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 取消热门
     * @param $advice_id
     * @return array
     */
    public function zxHotoff($advice_id)
    {
        $advice = ZxLists::find($advice_id);
        if (!$advice) {
            return ['status' => 2, 'message' => '出错了'];
        }
        $advice->hot = ZxLists::HOT_NO;
        if (!$advice->save()) {
            return ['status' => 2, 'message' => '失败'];
        }
        return ['status' => 1, 'message' => '成功'];
    }
	
	//资讯评论
	public function zxrepay($id, Request $request){
 
		if($request->isMethod('post')) {
            return response()->json(Datatables::Zxreplay($id));
        }
		
        return view('zixun.zxrepaly')->with('id', $id);
	}
	
	//添加评论
	public function zxaddrepay($id,Request $request){
		if ($request->isMethod('post')) {
            $return = Zxcomment::pinglunPublish($request);
            if ($return['status'] === 1) {
                return redirect('/zixun/repay/'.$id)->with('status', $return['status'])->with('message', $return['message']);
            }else{
				return back()->with('status', $return['status'])->with('message', $return['message']);
			}
        }

        return view('zixun.zxrepayPublish')->with('id', $id);
	}

	//删除资讯评论
	public function zxreplyDel($id, Request $request)
	{
		$gongju = Zxcomment::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此数据'];
        return Zxcomment::Del($id, $request);
	}
	
	//资讯评论回复
	public function zxhuifus($id, Request $request){

		if($request->isMethod('post')) {
            return response()->json(Datatables::Zxcomentreplay($id));
        }
        return view('zixun.zxcomentrepaly');
	}

	//删除资讯评论回复
	public function zxcomreplyDel($id, Request $request)
	{
		$gongju = Zacomrep::where('id', $id)->exists();
        if (!$gongju) return ['status' => 2, 'message' => '无此数据'];
        return Zacomrep::Del($id, $request);
	}
}
